//
//  HWStatusCell.m
//  01-黑马微博
//
//  Created by lanou3g on 15/9/12.
//  Copyright (c) 2015年 heima. All rights reserved.
//

#import "HWStatusCell.h"
#import "HWStatusFrame.h"
#import "HWUser.h"
#import "HWStatus.h"
#import "UIImageView+WebCache.h"
#import "HWPhoto.h"
#import "HWToolView.h"
#import "HWStatusPhotosView.h"
#import "HWIconView.h"
@interface HWStatusCell ()
/**原创微博*/
@property (nonatomic, weak) UIView *originalView;
/**头像*/
@property (nonatomic, weak) HWIconView *iconView;
/**配图**/
@property (nonatomic, weak) HWStatusPhotosView *photosView;
/**会员*/
@property (nonatomic, weak) UIImageView *vipView;
/**昵称*/
@property (nonatomic, weak) UILabel *nameLabel;
/**时间*/
@property (nonatomic, weak) UILabel *timeLabel;
/**来源*/
@property (nonatomic, weak) UILabel *sourceLabel;
/**正文*/
@property (nonatomic, weak) UILabel *contentLabel;
/**转发微博*/
@property (nonatomic, weak) UIView *retweetView;
/**转发正文*/
@property (nonatomic, weak) UILabel *retweetContentLabel;
/**转发配图**/
@property (nonatomic, weak) HWStatusPhotosView *retweetPhotosView;
/**cell底部条*/
@property (nonatomic, weak) HWToolView *toolBarView;
/***/

@end
@implementation HWStatusCell
+ (instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *ID = @"status";
    HWStatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[HWStatusCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"timeline_new_status_background"]];
//     点击cell不变色
        self.selectionStyle = UITableViewCellSelectionStyleNone;
//    设置原创微博的布局
        [self setupOriginal_status];
//        设置转发微博布局
        [self setupRetweeted_status];
//        设置工具条布局
        [self setupToolBarView];
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}
/**
 *  设置toolView
 *
 */
- (void)setupToolBarView{
    HWToolView *toolBarView = [[HWToolView alloc] init];
    [self addSubview:toolBarView];
    self.toolBarView = toolBarView;
}
/**
 *  设置转发微博
 */
- (void)setupRetweeted_status{
/**设置转发微博的整体的背景view*/
    UIView *retweetView = [[UIView alloc] init];
    retweetView.backgroundColor = HWColor(240, 240, 255);
    [self.contentView addSubview:retweetView];
    self.retweetView = retweetView;
    /**设置正文*/
    UILabel *retweetContentLabel = [[UILabel alloc] init];
    retweetContentLabel.font = HWRetweetContentFont;
    retweetContentLabel.numberOfLines = 0;
    [self.retweetView addSubview:retweetContentLabel];
    self.retweetContentLabel = retweetContentLabel;

   /**设置转发的配图*/
    HWStatusPhotosView *retweetPhotosView = [[HWStatusPhotosView alloc] init];
    [self.retweetView addSubview:retweetPhotosView];
    self.retweetPhotosView = retweetPhotosView;
}
/**
 *  原创微博
 */
- (void)setupOriginal_status{
    /*原创微博**/
    UIView *originalView = [[UIView alloc] init];
    originalView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:originalView];
    self.originalView = originalView;
    /*头像**/
    HWIconView *iconView = [[HWIconView alloc] init];
    [originalView addSubview:iconView];
    self.iconView = iconView;
    /*配图**/
    HWStatusPhotosView *photosView = [[HWStatusPhotosView alloc] init];
    [originalView addSubview:photosView];
    self.photosView = photosView;
    /*会员**/
    UIImageView *vipView = [[UIImageView alloc] init];
    vipView.contentMode = UIViewContentModeCenter;
    [originalView addSubview:vipView];
    self.vipView = vipView;
    /*昵称**/
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.font = HWNameFont;
    [originalView addSubview:nameLabel];
    self.nameLabel = nameLabel;
    /*时间**/
    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.textColor = [UIColor orangeColor];
    timeLabel.font = HWCreat_atFont;
    [self.originalView addSubview:timeLabel];
    self.timeLabel = timeLabel;
    /*来源**/
    UILabel *sourceLabel = [[UILabel alloc] init];
    sourceLabel.font = HWSourceFont;
    [self.originalView addSubview:sourceLabel];
    self.sourceLabel = sourceLabel;
    /*正文**/
    UILabel *contentLabel = [[UILabel alloc] init];
    contentLabel.numberOfLines = 0;
    contentLabel.font = HWContentFont;
    [self.originalView addSubview:contentLabel];
    self.contentLabel = contentLabel;
}
- (void)setFrame:(CGRect)frame{
    frame.origin.y += 10;
    [super setFrame:frame];
}
/**
 *  重写setframe  让cell的y向下移动
 */
- (void)setStatusFrame:(HWStatusFrame *)statusFrame{
    _statusFrame = statusFrame;
    [self setupFrameWithStatusFrame:statusFrame];
}
#pragma mark - 设置微博frame
- (void)setupFrameWithStatusFrame:(HWStatusFrame *)statusFrame{
    HWStatus *status = statusFrame.status;
    HWUser *user = status.user;
    /*原创微博**/
    self.originalView.frame = statusFrame.originalViewF;

    /*头像**/
    self.iconView.frame = statusFrame.iconViewF;
    self.iconView.user = user;
//    [self.iconView sd_setImageWithURL:[NSURL URLWithString:user.profile_image_url] placeholderImage:[UIImage imageNamed:@"avatar_default"]];
    /*配图**/
    self.photosView.frame = statusFrame.photosViewF;
    if (status.pic_urls.count > 0) {
        self.photosView.photos = status.pic_urls;
        self.photosView.frame = statusFrame.photosViewF;
        self.photosView.hidden = NO;
    }else{
        self.photosView.hidden = YES;
    }
    /*会员**/
    self.vipView.frame = statusFrame.vipViewF;
    if (user.isVip) {
        NSString *imageName = [NSString stringWithFormat:@"common_icon_membership_level%d",user.mbrank];
        self.vipView.hidden = NO;
        self.vipView.image = [UIImage imageNamed:imageName];
        self.nameLabel.textColor = [UIColor orangeColor];
    }else{
        self.vipView.hidden = YES;
        self.nameLabel.textColor = [UIColor blackColor];
    }
    /*昵称**/ 
    self.nameLabel.frame = statusFrame.nameLabelF;
    self.nameLabel.text = user.name;
    /*时间**/
    NSString *time = status.created_at;
    CGFloat timeX = CGRectGetMinX(statusFrame.nameLabelF);
    CGFloat timeY = CGRectGetMaxY(statusFrame.nameLabelF) + HWCellBorderWidth;
    CGSize timeSize = [time sizeWithFont:HWCreat_atFont maxSize:CGSizeMake(SCREEN_WIDTH, MAXFLOAT)];
    self.timeLabel.frame = CGRectMake(timeX, timeY, timeSize.width, timeSize.height);
    self.timeLabel.text = time;
    /*来源**/
    NSString *source = status.source;
    CGFloat sourceX = CGRectGetMaxX(self.timeLabel.frame) + HWCellBorderWidth;
    CGFloat sourceY = CGRectGetMinY(self.timeLabel.frame);
    CGSize sourceSize = [source sizeWithFont:HWSourceFont maxSize:CGSizeMake(SCREEN_WIDTH, MAXFLOAT)];
    self.sourceLabel.frame = CGRectMake(sourceX, sourceY, sourceSize.width, timeSize.height);
    self.sourceLabel.text = source;
    /*正文**/
    self.contentLabel.text = status.text;
    self.contentLabel.frame = statusFrame.contentLabelF;
    
    /**转发配图*/
    if (status.retweeted_status) {
        /**转发微博*/
        self.retweetView.frame = statusFrame.retweetViewF;
        self.retweetPhotosView.frame = statusFrame.retweetPhotosViewF;
        self.retweetView.hidden = NO;
        /**转发正文*/
        NSString *retweetText = [NSString stringWithFormat:@"@%@:%@",status.retweeted_status.user.name,status.retweeted_status.text];
        self.retweetContentLabel.text = retweetText;
        self.retweetContentLabel.frame = statusFrame.retweetContentLabelF;
        if (status.retweeted_status.pic_urls.count) {
            self.retweetPhotosView.photos = status.retweeted_status.pic_urls;
            self.retweetPhotosView.frame = statusFrame.retweetPhotosViewF;
            self.retweetPhotosView.hidden = NO;
        }else{
            self.retweetPhotosView.hidden = YES;
        }
    }else{
        self.retweetView.hidden = YES;
    }
    /**设置toolBarView*/
    self.toolBarView.frame = statusFrame.toolBarViewF;
    self.toolBarView.status = status;
}
@end
